<?php 	
/**
 * 投票管理
 */  
class vote {

	public function getRule($param){
	    $retVal = Session::init();
	    if (!$retVal) {
	        Output::instance()->resultCode(code_access_denied);
	        return;
	    }
	    $userName=Session::$user['userName'];
	    $customerId = Session::$user['customerId'];
	    Session::switchCustomer(0);
	    $vendorModel=M("micontent");
	    Session::switchCustomer($customerId);
	    $result=self::getVoteConfig($param);
	    if(!$result["code"]) {
	    	Output::instance()->module('vote',"rule")->object($result);
	    } else {
	    	Output::instance()->resultCode($result["code"],$result["msg"]);
	    }
	}
	public function getPrize($param){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$userName=Session::$user['userName'];
		$customerId = Session::$user['customerId'];
		Session::switchCustomer(0);
		$vendorModel=M("micontent");
		Session::switchCustomer($customerId);
		$result=self::getVoteConfig($param);
		if(!$result["code"]) {
			Output::instance()->module('vote',"prize")->object($result);
		} else {
			Output::instance()->resultCode($result["code"],$result["msg"]);
		}
	}
	public function getIndex($param){
	    $retVal = Session::init();
	    if (!$retVal) {
	        Output::instance()->resultCode(code_access_denied);
	        return;
	    }
	    $userName=Session::$user['userName'];
	    $customerId = Session::$user['customerId'];
	    Session::switchCustomer(0);
	    Session::switchCustomer($customerId);
	    $result=self::getVoteConfig($param);
	    if(!$result["code"]) {
	    	Output::instance()->module('vote',"index")->object($result);
	    } else {
	    	Output::instance()->resultCode($result["code"],$result["msg"]);
	    }
	}
	public function getList($param){
	    $retVal = Session::init();
	    if (!$retVal) {
	        Output::instance()->resultCode(code_access_denied);
	        return;
	    }
	    $userName=Session::$user['userName'];
	    $customerId = Session::$user['customerId'];
	    Session::switchCustomer(0);
	    Session::switchCustomer($customerId);
	    $result=self::getVoteConfig($param);
	    if(!$result["code"]) {
	    	Output::instance()->module('vote',"list")->object($result);
	    } else {
	    	Output::instance()->resultCode($result["code"],$result["msg"]);
	    }
	}
	
	public function getActivityVoteById($param){
	    $retVal = Session::init();
	    if (!$retVal) {
	        Output::instance()->resultCode(code_access_denied);
	        return;
	    }
	    $customerId = Session::$user['customerId'];	  
	    $uId = Session::$user['uid'];
	    Session::switchCustomer(0);
	    $mavModel=M("miactivity_vote_regpeople");
	    $where['id']=$param->id;	    	    
	    Session::switchCustomer($customerId);
        $arr= $mavModel->where($where)->find();	   
        //echo $mavModel->getLastSql();
        if($arr){
            Output::instance()->module('vote',"detail")->object($arr);  
            
        }else{
            Output::instance()->resultCode(code_access_denied);
        }
    	  
	}
	
	public function getrankinglist($param,$start,$limit){
	    $retVal = Session::init();
	    if (!$retVal) {
	        Output::instance()->resultCode(code_access_denied);
	        return;
	    }
	    $customerId = Session::$user['customerId'];
	    $uId = Session::$user['uid'];
	    Session::switchCustomer(0);
	    $mavModel=M("miactivity_vote_regpeople");
	
	    $pageStart=$start*$limit;
	
	    $where["activityId"]=array('eq',$param->actId);
	
	    $mavresList= $mavModel->alias("a")
	    ->where($where)
	    ->order("votecount desc")
	    ->limit($pageStart, $limit)
	    ->select();
		foreach($mavresList as $key=>$value){
			if($value["picurl"]){
				$mavresList[$key]["picurl"]=explode(',',$value["picurl"]);
			}
		}
	
	    $count =$mavModel->alias("mav")	  
	    ->where($where)
	    ->order("votecount desc")
	    ->count();
	  
	    Session::switchCustomer($customerId);
	    $mavres=new StdClass();
	    $mavres->count=$count;
	    $mavres->pageCount=getPageCount($count,$limit);
	    $mavres->list=$mavresList;
	    Output::instance()->object($mavres);
	}
	public function getDetail($param){
	    $retVal = Session::init();
	    if (!$retVal) {
	        Output::instance()->resultCode(code_access_denied);
	        return;
	    }
	    $userName=Session::$user['userName'];
	    $customerId = Session::$user['customerId'];
	    Session::switchCustomer(0);
	    $mRegModel=M("miactivity_vote_regpeople");
	    $mVoteModel=M("miactivity_vote_votepeople");
	    $id=$param->id;
	    $actId=$param->actId;
	    $regInfo=$mRegModel->alias("a")
	    ->field("a.*")
	    ->where("a.id='$id' and a.activityId=$actId and a.deleted=0")
	    ->find();
	    Session::switchCustomer($customerId);
	    $result=self::getVoteConfig($param);
	    $voteResult=$mVoteModel->alias("a")
	    ->join("inner join wx_miactivity_vote_regpeople b on a.rId=b.id")
	    ->join("inner join wx_miactivity c ON c.id=b.activityId ")
	    ->where("a.fromUserName='$userName' and b.id=$id and c.fromCustomerId=$customerId and c.id=$actId")->find();
	    Log::write($mVoteModel->getlastsql());
	    if($voteResult && count($voteResult)>0){
	    	$result["vote"]=1;
	    } else {
	    	$result["vote"]=0;
	    }
	    $result["shareTitle"]=str_replace("{name}",$regInfo["name"],C("ACT_DETAIL_SHARE_TITLE"));
	    $result["shareDes"]=C("ACT_DETAIL_SHARE_DEC");
	    if($regInfo){
			if($regInfo['picurl']){
				$regInfo['picurl']=explode(',',$regInfo['picurl']);
			}
	    	$result["regInfo"]=$regInfo;
	    } else {
	    	Output::instance()->resultCode(1,"记录不存在！");
	    }
	    if(!$result["code"]) {
	    	Output::instance()->module('vote',"detail")->object($result);
	    } else {
	    	Output::instance()->resultCode($result["code"],$result["msg"]);
	    }
	}
	public function getReg($param){
	    $retVal = Session::init();
	    if (!$retVal) {
	        Output::instance()->resultCode(code_access_denied);
	        return;
	    }
	    $userName=Session::$user['userName'];
	    $customerId = Session::$user['customerId'];
	    Session::switchCustomer(0);
	    $mRegModel=M("miactivity_vote_regpeople");
	    $regInfo=$mRegModel->where("fromUserName='$userName'")->find();
	    Session::switchCustomer($customerId);
	    $result=self::getVoteConfig($param);
	    if($regInfo){
			if($regInfo['picurl']){
				$regInfo['picurl']=explode(',',$regInfo['picurl']);
			}
	    	$result["regInfo"]=$regInfo;
	    }
	    if(!$result["code"]) {
	    	Output::instance()->module('vote',"reg")->object($result);
	    } else {
	    	Output::instance()->resultCode($result["code"],$result["msg"]);
	    }
	}
	public function saveReg($param){
		$retVal = Session::init();
	    if (!$retVal) {
	        Output::instance()->resultCode(code_access_denied);
	        return;
	    }
		$userName=Session::$user['userName'];
		$uid = Session::$user['userId'];
	    $customerId = Session::$user['customerId'];
	    Session::switchCustomer(0);
	    $mRegModel=M("miactivity_vote_regpeople");
	    Session::switchCustomer($customerId);
	    $id=$param->id;
	    $time=date('Y-m-d H:i:s');
	    $addData=array(
	    	"activityId"=>$param->actId,
	    	"name"=>$param->name,
	    	"phone"=>$param->phone,
	    	"picurl"=>$param->picurl,
			"weixin"=>$param->weixin,
	    	"fromCustomerId"=>$customerId,
	    	"quanId"=>$param->qId,
	    	"uId"=>$uid,
	    	"sId"=>$param->sId,
	    	"fromUserName"=>$userName,
	    	"createTime"=>$time,
	    	"deleted"=>0
	    );
	    if(!$id){
	    	$result=$mRegModel->add($addData);
	    	$id=$result;
	    } else {
	    	$result=$mRegModel->where("id=$id")->save($addData);
	    }
	    if($result!==false){
	    	$out=new StdClass();
	    	$out->code=0;
	    	$out->id=$id;
	    	Output::instance()->object($out);
	    }
	}
	public function addvote($param){
	    $retVal = Session::init();
	    if (!$retVal) {
	        Output::instance()->resultCode(code_access_denied);
	        return;
	    }
	    $cid = Session::$user["customerId"];
	    $weixinUserModel =M("user_weixin_info");
	    Session::switchCustomer(0);
	    
	    $regModel = M("miactivity_vote_regpeople");
	    $voteModel = M("miactivity_vote_votepeople");	    
	    
	    $fromUserName =Session::$user['userName'];
	    $votewhere['rId']=$param->rId;
	    $votewhere['fromCustomerId']=$cid;
	    $votewhere['fromUserName']=$fromUserName;
	    $votres=$voteModel->where($votewhere)->find();
	    
	    
	    
	    $weixinwhere['openid']=$fromUserName;
	    $weixinres=$weixinUserModel->where($weixinwhere)->find();
	    if($votres || !$weixinres){
	        Output::instance()->resultCode(1,'你已经投过票了');
	        return;
	    }else{	     
	        $votewhere['createTime']=date("Y-m-d H:i:s");
	        $votewhere['deleted']=0;
	        $voteModel->add($votewhere);	        
	        $regwhere['id']=$param->rId;
	        $regwhere['fromCustomerId']=$cid;
	        $result=$regModel->where($regwhere)->setInc('votecount',1);	        
	        $resultcount=$regModel->where($regwhere)->find();
	    }
	    Session::switchCustomer($cid);
	    if ($result !== false) {
	    	$out=new StdClass();
	    	$out->code=0;
	    	$out->votecount=$resultcount['votecount'];
	        Output::instance()->object($out);
	    } else {
	        Output::instance()->resultCode(code_sql_error);
	    }
	    
	  
	
	}
	public function getVoteConfig($param){
		$retVal = Session::init();
	    if (!$retVal) {
	        Output::instance()->resultCode(code_access_denied);
	        return;
	    }
		$userName=Session::$user['userName'];
		$actId=$param->actId;
		$uid = Session::$user['userId'];
	    $customerId = Session::$user['customerId'];
	    Session::switchCustomer(0);
	    $actModel=M("miactivity");
	    Session::switchCustomer($customerId);
	    $fromUId=$param->fromUId;
		if($fromUId){
			$uid=$fromUId;
		}
	    $where["a.deleted"]=array('eq',0);
	    $where["a.status"]=array('eq',1);
	    $where["a.id"]=array('eq',$actId);
	    $where["a.fromCustomerId"]=array('eq',$customerId);
	    $curtime= strtotime(date('Y-m-d 11:59:59'));
	    $result=$actModel->alias("a")
	    ->join("inner join wx_mishare b on a.id=b.moduleId and b.module='miactivity'")
	    ->field("a.*,b.id as sId")
	    ->join("inner join wx_mishare_material c on c.sId=b.id")
	    ->where($where)
	    ->find();
	    
		if($result){
	    	$json=json_decode($result["jsonstr"]);
	    	foreach ($json as $key => $value) {
	    		$va["src"]=$value->src;
	    		$va["info"]=$value->info;
	    		$jsonArr[$key]=$va;
	    	}
	    	$result["jsonstr"]=$jsonArr;
	    	$startTime=strtotime($result["startTime"]);
	    	$endTime=strtotime($result["endTime"]);
	    	$result["actStatus"]="0";
	    	if($startTime > $curtime){
	    		$result["actStatus"]=1;
	    	}
	    	if($endTime < $curtime){
	    		$result["actStatus"]=2;
	    	}
	    } else {
	    	$result["code"]=1;
	    	$result["msg"]="活动不存在！";
	    	return $result;
	    }
	    $Guid = new Guid();   
		$guid = $Guid->toString();//分享用的id
	    $result["fromUId"]=$uid;
	    $result["guid"]=$guid;
	    $result["actId"]=$actId;
	    return $result;
	}
}